global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/table_A5_correlation_matrix.log, replace name(tabl)

capture noi {
	
	* table correlation matrix
	*load and restrict to sample
	use ${final_dir}/regression_dataset${weight_window}${tf}, clear
	mmerge BvD using ${final_dir}/bvd_list_regfirms_auto95, unmatched(none)
	egen yearctry = group(year country_shr_1995) if year <= 2009
	egen yearindustry = group(year industry) if year <= 2009
	egen yearctryindustry = group(year industry country_shr_1995) if year <= 2009

	clonevar LSW = lswMPm_1995_a
	clonevar HSW = hswMPm_1995_a
	clonevar MSW = mswMPm_1995_a	
	clonevar VAEMP = vaempMPm_1995_a
	clonevar GDPGAP = lngdpgap_1995_a
	clonevar GDPPC = gdppcMPm_1995_a
	clonevar spilloversown = spill${depvar}${ttt}_1995_a
	clonevar spilloversother = spillN${depvar}${ttt}_1995_a
	clonevar spilloversownzero = spill${depvar}${ttt}_1995_a0
	clonevar spilloversotherzero = spillN${depvar}${ttt}_1995_a0
	clonevar stockown = k${depvar}_${ttt} 
	clonevar stockownzero = k${depvar}_${ttt}0  
	clonevar stockother = kNOT_${depvar}_${ttt} 
	clonevar stockotherzero = kNOT_${depvar}_${ttt}0
	bys lse_id : egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+2 & year <= 2009+2
	bys lse_id : egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
	drop _total_${depvar}_${ttt}_1995
	sort lse_id year

	ppmlhdfe F2.${depvar}_${ttt} LSW HSW GDPGAP GDPPC stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry) vce(cluster lse_id)
	keep if e(sample) 	

	* output table (via texdoc)

	texdoc init ${tab_dir}/appendix/Table_A5_correlation_matrix.tex, replace
	texdoc stlog, nolog

	local mvars LSW MSW HSW GDPGAP GDPPC VAEMP

	* residualize and store standard deviation
	foreach mvar1 of local mvars {
		rename `mvar1' var_`mvar1'	
		qui reghdfe var_`mvar1', vce(r) absorb(lse_id yearindustry) residuals
		predict `mvar1', residuals
		estpost summarize `mvar1'
		sleep 1000
		texdoc local sd_`mvar1' : display %10.3f e(sd)[1,1]
	}
	* correlation matrix of residuals
	foreach mvar1 of local mvars {
		foreach mvar2 of local mvars {
			estpost corr `mvar1' `mvar2'
			sleep 1000
			texdoc local `mvar1'_`mvar2' : display %10.3f e(b)[1,1]
		}
	}

	* output table (via texdoc)
	texdoc local notes "This table shows the correlation of residuals for the auto95 baseline regression sample, controlling for firm and year-industry fixed effects. The last row shows the standard deviation of the residual variables."
	texdoc stlog close
	include ${code_dir}/config/texdoctools.do
	tex `texpreamble'
	/*tex
	\begin{tabular}{@{}l*{6}{S[table-format = 1.3]}@{}}
	\toprule 
	& \mc{\makecell[c]{Low-skill \\ wage}} & \mc{\makecell{Middle-skill \\ wage}} & \mc{\makecell{High-skill \\wage}} & \mc{\makecell{GDP \\ gap}} & \mc{\makecell{GDP per \\capita}} & \mc{\makecell{Labor \\ productivity}} \\
	\midrule
	Low-skill wage     &	`LSW_LSW'	&	&		&	 &	 &		\\
	Middle-skill wage   &	`LSW_MSW'	&	`MSW_MSW'	&		&	 &	 &		\\
	High-skill wage     &	`LSW_HSW'	&	`MSW_HSW'	&	`HSW_HSW'	& & &	\\
	GDP gap             &	`LSW_GDPGAP'	&	`MSW_GDPGAP'	&	`HSW_GDPGAP'	&	`GDPGAP_GDPGAP' & &	\\
	GDP per capita      &	`LSW_GDPPC'	&	`MSW_GDPPC'	&	`HSW_GDPPC'	&	`GDPGAP_GDPPC' &	`GDPPC_GDPPC' &		\\
	Labor productivity  &	`LSW_VAEMP'	&	`MSW_VAEMP'	&	`HSW_VAEMP'	&	`GDPGAP_VAEMP' &	`GDPPC_VAEMP' &	`VAEMP_VAEMP'	\\
	Standard deviation  & `sd_LSW' &  `sd_MSW' &   `sd_HSW' &    `sd_GDPGAP' &   `sd_GDPPC' &    `sd_VAEMP' \\
	\bottomrule
	\end{tabular}
	tex*/
	tex `texpostamble'
	texdoc close

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}